package com.gxuwz.stats.service.excelNewHandler;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.gxuwz.stats.entity.AcademicWork;
import com.gxuwz.stats.entity.dto.AcademicWorkNewImportDTO;
import com.gxuwz.stats.service.excelhandler.ImportResultProvider;
import com.gxuwz.stats.service.impl.AcademicWorkServiceImpl;

import java.util.ArrayList;
import java.util.List;

public class AcademicWorkGxunExcelListener extends AnalysisEventListener<AcademicWorkNewImportDTO> implements ImportResultProvider {
    private final List<AcademicWork> dataList = new ArrayList<>();
    private int ignoredCount = 0;
    private final AcademicWorkServiceImpl workloadService;
    private boolean stopProcessing = false;

    private final String currentUsername;
    private final String currentRole;
    private final String currentDepartment;

    public AcademicWorkGxunExcelListener(AcademicWorkServiceImpl workloadService, String username, String role, String department) {
        this.workloadService = workloadService;
        this.currentUsername = username;
        this.currentRole = role;
        this.currentDepartment = department;
    }

    @Override
    public void invoke(AcademicWorkNewImportDTO dto, AnalysisContext context) {
        if (stopProcessing) {
            return;
        }
        // 检查姓名是否为“说明”
        if (dto.getUsername() == null || dto.getUsername().trim().isEmpty()) {
            return;
        }
        if (dto.getUsername().contains("说明") || dto.getUsername().length() > 12) {
            stopProcessing = true;
            return;
        }
        if ("普通用户".equals(currentRole)) {
            if (!dto.getUsername().equals(currentUsername)) {
                ignoredCount++;
                return;
            }
        } else if ("二级学院管理员".equals(currentRole)) {
            // 如果DTO有department字段才校验
            if (dto.getDepartment() != null && !dto.getDepartment().equals(currentDepartment)) {
                ignoredCount++;
                return;
            }
        }

        // DTO转AcademicWork实体，并合并“级别+类型+类别”
        AcademicWork entity = new AcademicWork();
        entity.setUsername(dto.getUsername());
        entity.setWorkName(dto.getWorkName());
        entity.setPublisher(dto.getPublisher());
        entity.setPublishDate(dto.getPublishDate());
        entity.setIsbn(dto.getIsbn());
        StringBuilder sb = new StringBuilder();
        if (dto.getLevel() != null) sb.append(dto.getLevel());
        if (dto.getType() != null) sb.append(dto.getType());
        if (dto.getCategory() != null && !dto.getCategory().isEmpty()) {
            sb.append(",");
            sb.append(dto.getCategory());
        }
        String category = sb.toString();
        entity.setAcademicWorkCategory(category);
        // 其他字段可按需补充

        dataList.add(entity);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        workloadService.saveAllFromDTO(dataList);
    }

    @Override
    public int getImportedCount() { return dataList.size(); }
    @Override
    public int getIgnoredCount() { return ignoredCount; }
}